package org.jsola.contract.helper;

import org.jsola.common.SafeKit;
import org.jsola.contract.dto.TemplateVariableAddDTO;
import org.jsola.contract.dto.TemplateVariableUpdateDTO;
import org.jsola.contract.entity.TemplateVariableDO;
import org.jsola.contract.query.TemplateVariableQuery;
import org.jsola.contract.service.ITemplateVariableService;
import org.jsola.contract.vo.TemplateVariableVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

import static org.jsola.contract.core.AbstractTest.SITE_ID;



/**
 * 合同模板关联变量
 *
 * @author JanusMix
 */
@Component
public class TemplateVariableTestHelper extends BaseTestHelper {
    
    @Autowired
    private ITemplateVariableService templateVariableService;
    @Autowired
    private VariableTestHelper variableTestHelper;

    /**
     * 生成add测试数据
     * @return TemplateVariableAddDTO
     */
    public TemplateVariableAddDTO getTemplateVariableAddDTO(){
        TemplateVariableAddDTO templateVariableAddDTO = new TemplateVariableAddDTO();
        templateVariableAddDTO.setTemplateId(SafeKit.getLong(getRandomValue("bigint",11)));
        templateVariableAddDTO.setVariableId(variableTestHelper.saveVariableExample().getId());
        return templateVariableAddDTO;
    }


    /**
     * 生成update测试数据
     * @return TemplateVariableUpdateDTO
     */
    public TemplateVariableUpdateDTO getTemplateVariableUpdateDTO(TemplateVariableVO templateVariableVO){
        return templateVariableService.selectDOById(templateVariableVO.getId(), SITE_ID).to(TemplateVariableUpdateDTO.class);
    }

    /**
     * 生成查询数据
     * @return TemplateVariableQuery
     */
    public TemplateVariableQuery getTemplateVariableQuery() {
        TemplateVariableQuery templateVariableQuery = new TemplateVariableQuery();
        List<TemplateVariableDO> templateVariableDOList = templateVariableService.list(new TemplateVariableDO(),SITE_ID);
        if (!CollectionUtils.isEmpty(templateVariableDOList)) {
            TemplateVariableDO templateVariableDO = templateVariableDOList.get(0);
            templateVariableQuery = templateVariableDO.to(TemplateVariableQuery.class);
        }
        return templateVariableQuery;
    }

    /**
     * 保存示例
     * @return TemplateVariableVO
     */
    public TemplateVariableVO saveTemplateVariableExample(){
        TemplateVariableAddDTO addDTO = this.getTemplateVariableAddDTO();
        TemplateVariableVO templateVariableVO = templateVariableService.save(addDTO,buildTokenUser());
        return templateVariableVO;
    }


}

